package com.lk.auth.config;


import com.lk.auth.jwt.ImoocJwtTokenEnhancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

//@Configuration
public class TokenStoreConfig {

//	@Autowired
//	private RedisConnectionFactory redisConnectionFactory;
	
//	@Bean
//	@ConditionalOnProperty(prefix="imooc.security.oauth2", name="store-type", havingValue="redis")
//	public TokenStore redisTokenStore() {
//		return new RedisTokenStore(redisConnectionFactory);
//	}
	
	@Configuration
	@ConditionalOnProperty(prefix="imooc.security.oauth2", name="store-type", havingValue="jwt", matchIfMissing=true)
	public static class JwtTokenConfig {
		
//		@Autowired
//		private SecurityProperties securityProperties;
//
		@Bean
		public TokenStore jwtTokenStore() {
			return new JwtTokenStore(jwtAccessTokenConverter());
		}
		
		/**
		 * 转换器， 设置jwt密签key
		 * @return
		 */
		@Bean
		public JwtAccessTokenConverter jwtAccessTokenConverter() {
			JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
			converter.setSigningKey("coohot");
			return converter;
		}
		
		@Bean
		@ConditionalOnMissingBean(name = "jwtTokenEnhancer")
		public TokenEnhancer jwtTokenEnhancer() {
			return new ImoocJwtTokenEnhancer();
		}
	}
}
